package com.chis.netty.simplehttp;



import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 
 * @Description 
 * @Company zwx
 * @author wlj
 * @version 2020年03月15日
 */
public class ThreadTest implements Runnable {
    private CyclicBarrier cyclicBarrier;
    private String url = "http://127.0.0.1:9211/test";

    public ThreadTest(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {

        try {
            // 等待所有任务准备就绪
            cyclicBarrier.await();
            // 测试内容
            System.out.println(" 调用时间："+new Date().getTime());
            Long l1 = new Date().getTime();
            try {
                HttpClient.doPost(url, "====测试====");
            } catch (Exception e){
                System.out.println(e.getMessage());
            }
            Long l2 = new Date().getTime();
            System.out.println("接口地址："+url+" 本次耗时："+(l2-l1));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        // 并发次数
        int testCount = 200;
        CyclicBarrier cyclicBarrier = new CyclicBarrier(testCount);
        ArrayList<Future<String>> results = new ArrayList<>();
        ExecutorService executorService = Executors.newFixedThreadPool(testCount);
        for (int i = 0; i < testCount; i++) {
            executorService.execute(new ThreadTest(cyclicBarrier));
        }
        executorService.shutdown();
        while (!executorService.isTerminated()) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

